/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.bookingmanager.restclient;

import com.sun.jersey.api.client.AsyncWebResource;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.api.json.JSONConfiguration;
import cz.muni.fi.pa165.bookingmanager.dto.CustomerDTO;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JOptionPane;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author Radek Koubsky
 */
public class CustomerRestClient {
    private Client client;
    private WebResource resource;
    
    public CustomerRestClient(){
        ClientConfig config = new DefaultClientConfig();
        client = Client.create(config);
        client.addFilter(new HTTPBasicAuthFilter("rest", "rest"));
        resource = client.resource(URIHelper.getURL());
    }

    public void createCustomer(CustomerDTO customer) {
        try{resource.path("customer").accept(MediaType.APPLICATION_JSON).post(customer);}
        catch(ClientHandlerException ex){
            JOptionPane.showMessageDialog(null,"server is unavailable");
            System.exit(-1);           
        }
    }

    public void updateCustomer(CustomerDTO customer) {
        try {resource.path("customer").accept(MediaType.APPLICATION_JSON).put(customer);}
        catch(ClientHandlerException ex){
            JOptionPane.showMessageDialog(null,"server is unavailable");
            System.exit(-1);           
        }
    }

    public void deleteCustomer(CustomerDTO customer) {
        try {resource.path("customer").path("delete").accept(MediaType.APPLICATION_JSON).post(customer);}
        catch(ClientHandlerException ex){
            JOptionPane.showMessageDialog(null,"server is unavailable");
            System.exit(-1);           
        }
    }

    public CustomerDTO getCustomerById(Long id) {
        return resource.path("customer").path("{id}").accept(MediaType.APPLICATION_JSON)
                .get(CustomerDTO.class);
    }

    public List<CustomerDTO> getCustomersByName(String name) {
        List<CustomerDTO> list = new ArrayList<CustomerDTO>();
        try{
        list=resource.path("customer").path("allByName-{name}").accept(MediaType.APPLICATION_JSON)
                .get(new GenericType<List<CustomerDTO>>() {
        });
        }
        catch(ClientHandlerException ex){
            JOptionPane.showMessageDialog(null,"server is unavailable");
            System.exit(-1);           
        }
        return Collections.unmodifiableList(list);

    }

    public List<CustomerDTO> getCustomers() {
        List<CustomerDTO> list = new ArrayList<CustomerDTO>();
        try{
        list = resource.path("customer").path("all").accept(MediaType.APPLICATION_JSON)
                .get(new GenericType<List<CustomerDTO>>() {
        });
        }
        catch(ClientHandlerException ex){
            JOptionPane.showMessageDialog(null,"server is unavailable");
            System.exit(-1);           
        }
        return Collections.unmodifiableList(list);
    }
}
